package com.xz.disk.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @PACKAGE_NAME: com.xz.disk.utils
 * @NAME: AppContext [javaproject]
 * @author：ASUS 肖舟 XiaoZhou
 * @datetime 创建于： 2020/8/2 11:35 星期日
 */
@Component
public class AppContext implements ApplicationContextAware {

    static ApplicationContext applicationContext;

    @Value("${path.rootPath}")
    private String rootPath;

    // 后缀名对应图标
    private static Map<String, String> icons;

    public static Map<String, String> getIcons() {
        return icons;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AppContext.applicationContext = applicationContext;
        init();
    }

    void init() {
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        java.io.File file = applicationHome.getDir();
        String path = file.getAbsolutePath();
        System.out.println("***********************************************************");
        System.out.println(path);
        System.out.println("***********************************************************");


//        String path = null;
        try {
//            path = ResourceUtils.getURL("classpath:").getPath();

            path = path + File.separator + "storage" + File.separator;
            if (!FileUtil.exist(path)) {
                FileUtil.mkdir(path);
            }
            String path2 = path + "icon.conf";
            if (!FileUtil.exist(path2)) {
                FileUtil.touch(path2);
            }
            System.out.println("写----------->" + path2);
            String str = FileUtil.readFile(path2);
            if (StringUtils.isEmpty(str)) {
                StringBuffer sbf = new StringBuffer();
                sbf.append("dirctory=dirctory.png\r\n");
                sbf.append("zip=zip.png\r\n");
                sbf.append("rar=zip.png\r\n");
                sbf.append("7z=zip.png\r\n");
                sbf.append("tar=zip.png\r\n");
                sbf.append("txt=txt.png\r\n");
                sbf.append("xls=xls.png\r\n");
                sbf.append("xlsx=xls.png\r\n");
                sbf.append("ppt=ppt.png\r\n");
                sbf.append("pptx=ppt.png\r\n");
                sbf.append("doc=word.png\r\n");
                sbf.append("docx=word.png\r\n");
                sbf.append("js=js.png\r\n");
                sbf.append("html=html.png\r\n");
                sbf.append("css=3.png\r\n");
                sbf.append("java=java.png\r\n");
                sbf.append("jar=java.png\r\n");
                sbf.append("war=java.png\r\n");
                sbf.append("mp3=mp3.png\r\n");
                sbf.append("wav=mp3.png\r\n");
                sbf.append("mp4=mp4.png\r\n");
                sbf.append("3gp=mp4.png\r\n");
                sbf.append("mov=mp4.png\r\n");
                sbf.append("MOV=mp4.png\r\n");
                sbf.append("wmv=mp4.png\r\n");
                sbf.append("md=md.png\r\n");

                FileUtil.writeFile(path2, sbf.toString());


            }
            icons = new HashMap<>();
            System.out.println("读----------->" + path2);
            String str2 = FileUtil.readFile(path2);
            String[] iconsStr = str2.split("\r\n");
            for (String s : iconsStr) {
                if (s.indexOf("=") != -1) {
                    String key = s.split("=")[0];
                    String value = s.split("=")[1];
                    icons.put(key, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        String recycle_bin = rootPath + "回收站" + File.separator;
        if (!FileUtil.exist(recycle_bin)) {
            FileUtil.mkdir(recycle_bin);
        }
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return (T) applicationContext.getBean(name);
    }
}
